import java.util.*;

public class 字符串压缩 {
    
    public static void main(String[] args) {
        System.out.println(compressString("aabcccccaa"));
    }

    public static String compressString(String S) {
        

        if (null == S || S.isEmpty()) {

            return S;
        }

        StringBuilder builder = new StringBuilder();
        int count = 1;
        char c = S.charAt(0);

        for (int i = 1; i < S.length(); i++) {

            if (S.charAt(i) == c) {

                count += 1;
            } else {

                builder.append(c).append(count);
                c = S.charAt(i);
                count = 1;
            }
        }

        builder.append(c).append(count);

        if (S.length() > builder.toString().length()) {

            return builder.toString();
        }

        return S;

    }
}
